package com.igorkundovic.guitarstab.pages;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.CleanupRender;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.igorkundovic.guitarstab.User;
import com.igorkundovic.guitarstab.beans.UserBean;
import com.igorkundovic.guitarstab.service.UserService;



public class Login {

	   
	   @ApplicationState
	   private User user;
	   
	   @Property
	   @Persist
	   private String message;
	   @Inject
	   private UserService userService;
	   
	   @Property
	   @Persist
	   private User userBean;
	   
	   @SetupRender
	   public void setUp(){
	       userBean=new UserBean();
	   }
	   
	   @OnEvent(component="userlogin")
	   Object onSuccess(){
		   message=null;
	       User registredUser=userService.retrive(userBean);
	       if(registredUser!= null){
	           user=registredUser;
	           return Index.class;
	           
	       }
	       else{
	    	   message="Incorrect username or password!";
	       }
	       return this;
	   }
	 
	    @CleanupRender
		public void cleanUp(){
			message =null;
		}
	
	
}
